import { SearchOutlined, DownloadOutlined, PlusOutlined, VerticalAlignBottomOutlined, FileExcelOutlined, UploadOutlined, VideoCameraAddOutlined, BranchesOutlined, SettingOutlined } from '@ant-design/icons-vue'
import type { Component } from 'vue'


import { useRoute, useRouter } from 'vue-router';


interface btn {
    title: string,
    icon?: Function | Component,
    type?: string,
    danger?: boolean,
    shape?: string,
}

const tablePermission = ['edit', 'delete', 'info', 'breed', 'img', 'info',]
const permissionObj: Record<string, btn> = {
    query: {
        title: '查询',
        shape: 'circle',
        icon: SearchOutlined,
    },
    add: {
        title: '添加',
        shape: 'circle',
        icon: PlusOutlined,
        type: 'primary',
    },
    model: {
        title: '下载模板',
        shape: 'circle',
        icon: FileExcelOutlined,
    },
    import: {
        title: '上传数据',
        shape: 'circle',
        icon: UploadOutlined,
    },
    export: {
        title: '导出当前数据',
        shape: 'circle',
        icon: DownloadOutlined,
    },
    exportAll: {
        title: '导出全部数据',
        shape: 'circle',
        icon: VerticalAlignBottomOutlined,
    },
    setVideo: {
        title: '批量设置视频',
        shape: 'circle',
        icon: VideoCameraAddOutlined,
    },
    setVersion: {
        title: '批量升级',
        shape: 'circle',
        icon: BranchesOutlined,
    },
    href: {
        title: '自定义配置',
        shape: 'circle',
        icon: SettingOutlined,
    },
    downloadModel: {
        title: '下载模板',
        shape: 'circle',
        icon: FileExcelOutlined,
    },
    uploadModel: {
        title: '上传数据',
        shape: 'circle',
        icon: UploadOutlined,
    },
}
/**
 * 创建按钮组
 * @param arr 按钮名称数组
 * @param FnObj 包含按钮点击事件函数的对象
 * @returns 返回一个数组，数组中每个元素都代表一个按钮，包含按钮的标题、权限、形状、图标和点击事件函数
 */
function createPermissionBtns(permissionArr: string[], FnObj: any) {
    // 遍历按钮名称数组，生成按钮配置对象数组
    return permissionArr.map(item => {
        // 如果按钮权限不在预设的权限列表中，则生成并返回按钮配置对象
        if (!tablePermission.includes(item)) {
            return {
                title: permissionObj[item as keyof typeof permissionObj].title, // 按钮标题
                type: permissionObj[item as keyof typeof permissionObj]?.type,
                permission: item, // 按钮权限标识
                shape: permissionObj[item as keyof typeof permissionObj]?.shape, // 按钮形状
                danger: permissionObj[item as keyof typeof permissionObj]?.danger,
                icon: permissionObj[item as keyof typeof permissionObj]?.icon, // 按钮图标
                clickFn: FnObj[item] // 按钮点击事件函数
            }
        }
    })
}

function handleArr(arr: any[]) {
    return arr.filter((item: string) => Boolean(item));
}

export function createBtns(clickFnObj: any) {
    const router = useRouter()
    const routeName = useRoute().name
    const currentRouteMeta = router.getRoutes().filter(item => item.name == routeName)[0].meta.btnsPermission

    // console.log(currentRouteMeta)
    return {
        currentRouteMeta,
        Btns: handleArr(createPermissionBtns(currentRouteMeta as string[], clickFnObj))
    }
}